﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace Engine.Sgml {
    internal static class SgmlTextEncoder {

        public static void EncodeAttributeValue(string value, TextWriter writer) {
            Debug.Assert(value != null);
            Debug.Assert(writer != null);
            for (int i = 0; i < value.Length; i++) {
                char ch = value[i];
                switch (ch) {
                    case Characters.QuotationMark:
                        writer.Write(Characters.StartEntityReference);
                        writer.Write('q');
                        writer.Write('u');
                        writer.Write('o');
                        writer.Write('t');
                        writer.Write(Characters.EndEntityReference);
                        break;
                    default:
                        writer.Write(ch);
                        break;
                    }
                }
            }

        public static void EncodeChar(char ch, TextWriter writer) {
            Debug.Assert(writer != null);
            switch (ch) {
                case Characters.StartTagMarkup:
                    writer.Write(Characters.StartEntityReference);
                    writer.Write('l');
                    writer.Write('t');
                    writer.Write(Characters.EndEntityReference);
                    break;
                case Characters.EndTagMarkup:
                    writer.Write(Characters.StartEntityReference);
                    writer.Write('g');
                    writer.Write('t');
                    writer.Write(Characters.EndEntityReference);
                    break;
                case Characters.Ampersand:
                    writer.Write(Characters.StartEntityReference);
                    writer.Write('a');
                    writer.Write('m');
                    writer.Write('p');
                    writer.Write(Characters.EndEntityReference);
                    break;
                default:
                    writer.Write(ch);
                    break;
                }
            }
        }
    }
